package com.fancy.user.controller;

import cn.hutool.db.PageResult;
import com.fancy.common.dto.response.Result;
import com.fancy.imasclient.dto.FollowInfoDTO;
import com.fancy.imasclient.dto.FollowRequestDTO;
import com.fancy.imasclient.dto.UserCollectionResponseDTO;
import com.fancy.user.controller.vo.UserRegisterVO;
import com.fancy.user.dto.request.*;
import com.fancy.user.dto.response.*;
import com.fancy.user.service.UserService;
import com.fancy.user.utils.IpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/v1/users")
@Api(tags = "用户服务")
public class UserController {
    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result<UserRegisterResponseVO> register(@RequestBody UserRegisterVO vo) {
        UserRegisterRequestDTO dto = new UserRegisterRequestDTO();
        BeanUtils.copyProperties(vo, dto);
        return userService.register(dto);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginResponseVO> login(@RequestBody UserLoginRequestDTO dto,
                                           HttpServletRequest request,
                                           HttpServletResponse response) {
        // 获取客户端IP
        String clientIp = IpUtils.getClientIp(request);
        dto.setClientIp(clientIp);

        Result<UserLoginResponseVO> result = userService.login(dto);

        // 如果登录成功，将token���入响应头
        if (result.isSuccess() && result.getData() != null) {
            String token = result.getData().getToken();
            response.setHeader("Authorization", "Bearer " + token);
            response.setHeader("Access-Control-Expose-Headers", "Authorization");
        }

        return result;
    }

    @PostMapping("/logout")
    @ApiOperation("用户登出")
    public Result<Void> logout(@RequestHeader(value = "Authorization", required = false) String token,
                              HttpServletResponse response) {
        // 清除响应头中的token
        response.setHeader("Authorization", "");
        return Result.success(null);
    }

    @GetMapping("/{userId}")
    @ApiOperation("获取用户信息")
    public Result<UserDetailVO> getUserInfo(@PathVariable Long userId) {
        return userService.getUserInfo(userId);
    }

    @PutMapping("/{userId}")
    @ApiOperation("更新用��信息")
    public Result<Void> updateUserInfo(@PathVariable Long userId, @RequestBody UserUpdateRequestDTO dto) {
        return userService.updateUserInfo(userId, dto);
    }

    @PostMapping("/{userId}/membership")
    @ApiOperation("会员开通")
    public Result<MembershipPurchaseResponseVO> purchaseMembership(
            @PathVariable Long userId,
            @RequestBody MembershipPurchaseRequestDTO dto) {
        return userService.purchaseMembership(userId, dto);
    }

    @GetMapping("/{userId}/collections")
    @ApiOperation("获取用户收藏")
    public Result<PageResult<UserCollectionResponseDTO>> getUserCollections(
            @PathVariable Long userId,
            @RequestParam Integer type,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        return userService.getUserCollections(userId, type, page, size);
    }

    @PostMapping("/{userId}/follow")
    @ApiOperation("关注用户/歌手")
    public Result<Void> followTarget(@PathVariable Long userId, @RequestBody FollowRequestDTO dto) {
        return userService.followTarget(userId, dto);
    }

    @DeleteMapping("/{userId}/follow")
    @ApiOperation("取消关注")
    public Result<Void> unfollowTarget(@PathVariable Long userId, @RequestBody FollowRequestDTO dto) {
        return userService.unfollowTarget(userId, dto);
    }

    @GetMapping("/{userId}/following")
    @ApiOperation("获取关注列表")
    public Result<PageResult<FollowInfoDTO>> getFollowing(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        return userService.getFollowing(userId, page, size);
    }

    @GetMapping("/{userId}/followers")
    @ApiOperation("获取粉丝列表")
    public Result<PageResult<FollowInfoDTO>> getFollowers(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        return userService.getFollowers(userId, page, size);
    }

    @GetMapping("/membership/types")
    @ApiOperation("获取会员类型列表")
    public Result<List<MembershipTypeResponseVO>> getMembershipTypes() {
        return userService.getMembershipTypes();
    }

    @GetMapping("/membership/types/{typeCode}")
    @ApiOperation("获取会员类型详情")
    public Result<MembershipTypeDetailVO> getMembershipTypeDetail(@PathVariable String typeCode) {
        return userService.getMembershipTypeDetail(typeCode);
    }
}